﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;
using System.Data;

namespace SaleManagement.Web.Pages
{
  public partial class Payment : BasePage
  {

    private string m_InnerError = string.Empty;
    private string m_InnerProperties = string.Empty;
    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadPaymentDetail();
      }
    }

    protected void gvPayment_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string para = e.Parameters;
      if (para == "Payment")
      {
        m_InnerProperties = "Payment";
        int purchaseId, customerId;
        purchaseId = customerId = -1;
        int.TryParse(hfCustomerId.Value, out customerId);
        int.TryParse(hfPurchaseId.Value, out purchaseId);
        m_InnerError = DoPayment(purchaseId, customerId);
      }
      LoadPaymentDetail();
    }

    protected void gvPayment_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_InnerError;
      e.Properties["cpInnerProperty"] = m_InnerProperties;
    }


    protected void gvPayment_PageIndexChanged(object sender, EventArgs e)
    {
      LoadPaymentDetail();
    }


    protected void gvPaymentDetail_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Load")
      {
        int id = -1;
        int.TryParse(para[1], out id);
        LoadPurchaseDetail(id);
      }
    }


    #endregion

    #region Methods

    private void LoadPaymentDetail()
    {
      string customerCode = null;
      string customerName = null;
      string phone = null;
      bool stillDebt = false;
      stillDebt = cbxStillDebt.Checked;
      if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
      {
        customerCode = txtSearchCode.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchName.Text.Trim()))
      {
        customerName = txtSearchName.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchPhone.Text.Trim()))
      {
        phone = txtSearchPhone.Text.Trim();
      }

      DataSet ds = new CustomerService().SearchPayment(customerCode, customerName, phone,stillDebt);
      gvPayment.DataSource = ds;
      gvPayment.DataBind();
      ApplyPaymentMasterLayout();
    }

    private void ApplyPaymentMasterLayout()
    {
      gvPayment.BeginUpdate();
      try
      {
        gvPayment.ClearSort();
        gvPayment.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvPayment.Columns["CustomerId"]);
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvPayment.EndUpdate();
      }
      gvPayment.ExpandAll();
    }


    private void LoadPurchaseDetail(int id)
    {
      gvPaymentDetail.DataSource = new PaymentService().GetByPurchaseId(id);
      gvPaymentDetail.DataBind();
    }

    private string DoPayment(int purchaseId, int customerId)
    {
      try
      {
        Domains.Payment payment = new Domains.Payment();
        payment.PurchaseId = purchaseId;
        payment.Amount = Convert.ToInt32(txtMoney.Text);
        payment.Date = DateTime.Now;
        payment.UpdatedBy = CurrentUser.Username;
        payment.Notes = mmNote.Text;
        payment.UpdatedDate = DateTime.Now;
        payment.CustomerId = customerId;

        new PaymentService().Save(payment);
        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::DoPayment", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }

    #endregion

   

  }
}
